package com.china.produce.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.china.produce.service.OrderMQService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2022/1/28
 */
@Service("order_hello")
@Slf4j
public class OrderMQServiceImpl implements OrderMQService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void regCallback() {
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println(correlationData);
            }
        });
    }

    public void sendMsg()
    {
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("test", "123");

        jsonObject.put("id", 1000);

        rabbitTemplate.convertAndSend("fanout_exchange", "", JSONUtil.toJsonStr(jsonObject), new CorrelationData(jsonObject.getString("id")));
    }
}
